1
|
|
|
// jshint esversion: 8, -W030 |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
byteToHumanFileSize |
5
|
|
|
Convert bytes size to human readable format |
6
|
|
|
parameters |
7
|
|
|
bytes (integer) - bytes to convert |
8
|
|
|
si (boolean) - use standard international units |
9
|
|
|
*/ |
10
|
|
|
function byteToHumanFileSize(bytes, si = true) { |
11
|
|
|
var thresh = si ? 1000 : 1024; |
12
|
|
|
|
13
|
|
|
if (Math.abs(bytes) < thresh) return bytes + ' B'; |
14
|
|
|
|
15
|
|
|
var units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; |
16
|
|
|
var u = -1; |
17
|
|
|
|
18
|
|
|
do { |
19
|
|
|
bytes /= thresh; |
20
|
|
|
++u; |
21
|
|
|
} while (Math.abs(bytes) >= thresh && u < units.length - 1); |
22
|
|
|
|
23
|
|
|
return `${bytes.toFixed(1)} ${units[u]}`; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/* |
27
|
|
|
msToHumanTime |
28
|
|
|
Convert a given milliseconds to string in human readable format |
29
|
|
|
parameters |
30
|
|
|
duration (integer) - milliseconds to convert |
31
|
|
|
*/ |
32
|
|
|
function msToHumanTime(duration = 0) { |
33
|
|
|
const BASE = 10; |
34
|
|
|
|
35
|
|
|
if (parseInt(duration, BASE) <= 0 || isNaN(parseInt(duration, BASE))) return '-'; |
36
|
|
|
|
37
|
|
|
var parsedTime = { |
38
|
|
|
milliseconds: parseInt((duration % 1000), BASE), |
39
|
|
|
seconds: parseInt((duration / 1000).toFixed(6) % 60, BASE), |
40
|
|
|
minutes: parseInt((duration / (1000 * 60)).toFixed(6) % 60, BASE), |
41
|
|
|
hours: parseInt((duration / (1000 * 60 * 60)).toFixed(6) % 24, BASE), |
42
|
|
|
days: parseInt((duration / (1000 * 60 * 60 * 24)).toFixed(6) % 7, BASE), |
43
|
|
|
weeks: parseInt((duration / (1000 * 60 * 60 * 24 * 7)).toFixed(6) % 4, BASE), |
44
|
|
|
months: parseInt((duration / (1000 * 60 * 60 * 24 * 7 * 4)).toFixed(6) % 12, BASE), |
45
|
|
|
years: parseInt((duration / (1000 * 60 * 60 * 24 * 7 * 4 * 12)).toFixed(6) % 10, BASE) |
46
|
|
|
}, |
47
|
|
|
time = ''; |
48
|
|
|
|
49
|
|
|
time = compileString(parsedTime); |
50
|
|
|
|
51
|
|
|
return time; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/* |
55
|
|
|
compileString |
56
|
|
|
Compiles parsed time to a human readable string |
57
|
|
|
parameters |
58
|
|
|
parsedTime |
59
|
|
|
*/ |
60
|
|
|
function compileString(parsedTime) { |
61
|
|
|
var compiledString = '', |
62
|
|
|
suffixes = { // String suffixes by name |
63
|
|
|
milliseconds: 'ms', |
64
|
|
|
seconds: 's', |
65
|
|
|
minutes: 'm', |
66
|
|
|
hours: 'h', |
67
|
|
|
days: 'd', |
68
|
|
|
weeks: 'w', |
69
|
|
|
months: 'M', |
70
|
|
|
years: 'Y' |
71
|
|
|
}, |
72
|
|
|
orderedSuffixes = []; |
73
|
|
|
|
74
|
|
|
for (var unorderedParcel in parsedTime) orderedSuffixes.unshift(unorderedParcel); |
75
|
|
|
|
76
|
|
|
for (var parcel in orderedSuffixes) |
77
|
|
|
if (parsedTime[orderedSuffixes[parcel]] > 0) |
78
|
|
|
compiledString += `${parsedTime[orderedSuffixes[parcel]]} ${suffixes[orderedSuffixes[parcel]]} `; |
79
|
|
|
|
80
|
|
|
return compiledString; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/* |
84
|
|
|
getDate |
85
|
|
|
Parse a date from a source string |
86
|
|
|
parameters |
87
|
|
|
date (string/date) - Date or String to be date parsed |
88
|
|
|
*/ |
89
|
|
|
function getDate(date) { |
90
|
|
|
if (date === null || date === '' || date === false) return undefined; |
91
|
|
|
var parsed = new Date(Date.parse(date)).toUTCString(); |
92
|
|
|
if (parsed == 'Invalid Date') return date; |
93
|
|
|
|
94
|
|
|
return parsed; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
module.exports = { |
98
|
|
|
byteToHumanFileSize: byteToHumanFileSize, |
99
|
|
|
msToHumanTime: msToHumanTime, |
100
|
|
|
getDate: getDate |
101
|
|
|
}; |
102
|
|
|
|